package com.ggh.jinjilive.ui.create;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.create.ChooseRoom;
import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.mobile.create.StartLiveMobile;
import com.ggh.jinjilive.mobile.home.live.LiveCategory;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;
import com.huantansheng.easyphotos.models.album.entity.Photo;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class CreateLiveViewModel extends BaseViewModel {
    //选择的图片
    public ObservableField<Photo> photoObservableField = new ObservableField<>();
    //直播间类目列表
    public ObservableField<List<LiveCategory>> listMutableLiveData = new ObservableField<>();
    //存储选择的类目
    public ObservableField<LiveCategory> liveCategoryObservableField = new ObservableField<>();
    //房间类型
    public ObservableField<List<ChooseRoom>> chooseRoomLiveData = new ObservableField<>();
    //存储选择房间类型
    public ObservableField<ChooseRoom> chooseRoomObservableField = new ObservableField<>();
    //标题
    public ObservableField<String> titleObservableField = new ObservableField<>();
    //封面
    public ObservableField<String> stringObservableField = new ObservableField<>();
    //密码
    public ObservableField<String> passwordObservableField = new ObservableField<>();
    //付费金额 type=2时，必须存在
    public ObservableField<String> priceObservableField = new ObservableField<>();
    //每分钟扣费金额 type=3时，必须存在
    public ObservableField<String> price_minObservableField = new ObservableField<>();

    //开始直播回调
    public MutableLiveData<StartLiveMobile> StartLiveMobileMutableLiveData  = new MutableLiveData<>();

    public CreateLiveViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);
    }

    /**
     * 直播间类目列表
     */
    public void liveCateGory(){
        addSubscribe(apiCallInterface.liveCateGory().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<LiveCategory>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<LiveCategory> liveCategoryList = (List<LiveCategory>) resultEntity.getData();
                listMutableLiveData.set(liveCategoryList);
                if(liveCategoryList.size() > 0){
                    liveCategoryObservableField.set(liveCategoryList.get(0));
                }

            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                ToastUtils.make().show(netMsg);
            }

        }));
    }

    /**
     * 上传封面图接口
     */
    public void uploadimages(){
        File file = new File(photoObservableField.get().path);
        RequestBody fileRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), fileRequestBody);
        addSubscribe(apiCallInterface.uploadimages(body).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<CommonMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                CommonMobile commonMobile = (CommonMobile) resultEntity.getData();
                stringObservableField.set(commonMobile.getUrl());
                LogUtils.d(commonMobile.getUrl());
                openLive();
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                ToastUtils.make().show(netMsg);
            }

        }));
    }

    /**
     * 生成直播推流地址 接口
     */
    public void openLive(){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("title",titleObservableField.get());
        paramsa.put("cover",stringObservableField.get());
        paramsa.put("category",liveCategoryObservableField.get().getId());
        paramsa.put("type",chooseRoomObservableField.get().getID());
        paramsa.put("pass",passwordObservableField.get());// 房间密码 type=1时 必须存在
        paramsa.put("price",priceObservableField.get());// 付费金额 type=2时，必须存在
        paramsa.put("price_min",price_minObservableField.get());// 每分钟扣费金额 type=3时，必须存在

        addSubscribe(apiCallInterface.openLive(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<StartLiveMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                StartLiveMobile startLiveMobile = (StartLiveMobile) resultEntity.getData();
                StartLiveMobileMutableLiveData.setValue(startLiveMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                ToastUtils.make().show(netMsg);
            }

        }));
    }
}
